import net.java.games.jogl.GL;
import java.net.*;
import java.io.*;


class vector
{
    public float x,y,z;
}

class face
{
    public int f1,f2,f3;
}

public class model3d
 {
    int numVertices;
    int numFaces;
    vector vertices[];
    vector normals[];
    face faces[];
    int list;

    public model3d()
    {
        list=0;
    }

    public void read(String fileName)
    {
        String fileContent;
        int index;
        try
        {
            BufferedReader inputScript=new BufferedReader(new FileReader(fileName));

            fileContent=inputScript.readLine();
            numVertices=Integer.valueOf(fileContent);

            fileContent=inputScript.readLine();
            numFaces=Integer.valueOf(fileContent);

            vertices=new vector[numVertices];
            faces=new face[numFaces];
            normals=new vector[numVertices];

            for(index=0;index<numVertices;index++)
            {
                 vertices[index]=new vector();
                 fileContent=inputScript.readLine();
                 vertices[index].x=Float.valueOf(fileContent);

                 fileContent=inputScript.readLine();
                 vertices[index].y=Float.valueOf(fileContent);

                 fileContent=inputScript.readLine();
                 vertices[index].z=Float.valueOf(fileContent);
            }

            for(index=0;index<numFaces;index++)
            {
                 faces[index]=new face();
                 fileContent=inputScript.readLine();
                 faces[index].f1=Integer.valueOf(fileContent);

                 fileContent=inputScript.readLine();
                 faces[index].f2=Integer.valueOf(fileContent);

                 fileContent=inputScript.readLine();
                 faces[index].f3=Integer.valueOf(fileContent);
            }

            for(index=0;index<numVertices;index++)
            {
                 normals[index]=new vector();
                 fileContent=inputScript.readLine();
                 normals[index].x=Float.valueOf(fileContent);

                 fileContent=inputScript.readLine();
                 normals[index].y=Float.valueOf(fileContent);

                 fileContent=inputScript.readLine();
                 normals[index].z=Float.valueOf(fileContent);
            }

            inputScript.close();
        }catch(FileNotFoundException e)
        {
              System.out.println("Error : File \""+fileName+"\" not found. ");
              System.exit(1);
        }
        catch(IOException e)
        {
            System.out.println("Error : "+e.getMessage());
            System.exit(1);
        }
    }

    public void update(GL gl)
    {
        int index;

        list = gl.glGenLists (1);
        gl.glNewList(list, gl.GL_COMPILE);
        gl.glBegin(gl.GL_TRIANGLES);
            for(index=0;index<numFaces;index++)
            {
                    gl.glNormal3f(normals[faces[index].f1].x,normals[faces[index].f1].y,normals[faces[index].f1].z);
                    gl.glVertex3f(vertices[faces[index].f1].x,vertices[faces[index].f1].y,vertices[faces[index].f1].z);

                    gl.glNormal3f(normals[faces[index].f2].x,normals[faces[index].f2].y,normals[faces[index].f2].z);
                    gl.glVertex3f(vertices[faces[index].f2].x,vertices[faces[index].f2].y,vertices[faces[index].f2].z);

                    gl.glNormal3f(normals[faces[index].f3].x,normals[faces[index].f3].y,normals[faces[index].f3].z);
                    gl.glVertex3f(vertices[faces[index].f3].x,vertices[faces[index].f3].y,vertices[faces[index].f3].z);

            }
       gl.glEnd();
       gl.glEndList();


    }

    public void show(GL gl,float x, float y,float z)
    {
        gl.glPushMatrix();
            gl.glTranslatef(x, y, z);
            gl.glCallList(list);
        gl.glPopMatrix();
    }

    public void free(GL gl)
    {
        gl.glDeleteLists(list,1);
    }

}
